package miiteen.customsportal.login.service;

import cn.gov.customs.casp.sdk.h4a.entity.ObjectParentOrganizations;
import cn.gov.customs.casp.sdk.h4a.entity.ObjectsDetail;
import miiteen.customsportal.basic.beans.BasicPageBrowsing;
import miiteen.customsportal.basic.dao.BasicPageBrowsingDao;
import miiteen.customsportal.basic.service.BasicPageBrowsingService;
import miiteen.customsportal.login.beans.LoginOrganizationInfo;
import miiteen.customsportal.login.beans.LoginUserInfo;
import miiteen.customsportal.login.dao.LoginOrganizationInfoDao;
import miiteen.customsportal.login.dao.LoginUserInfoDao;
import miiteen.data.service.impl.BaseServiceImpl;
import miiteen.demo.utils.IPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("loginIndexService")
public class LoginIndexService {


    @Autowired
    private  LoginUserInfoService loginUserInfoService;
    @Autowired
    private   BasicPageBrowsingService basicPageBrowsingService;

    public  long saveLoginUser(ObjectsDetail objectDeatil, ObjectParentOrganizations[] objectParentOrganizations) throws IOException {
        LoginUserInfo loginUserInfo=new LoginUserInfo();
        loginUserInfo.setUserGuid(objectDeatil.getUser_guid()) ;
        loginUserInfo.setAllPathName(objectDeatil.getAll_path_name());
        loginUserInfo.setDisplayName( objectDeatil.getDisplay_name());
        loginUserInfo.setViewGuid(objectDeatil.getView_guid());
        loginUserInfo.setOrginalSort(objectDeatil.getOriginal_sort());
        loginUserInfo.setPersonId(objectDeatil.getPerson_id());
        loginUserInfo.setParentGuid(objectDeatil.getParent_guid());
        List<LoginOrganizationInfo> list=new ArrayList<>();
        for (ObjectParentOrganizations objectParentOrganizations1:objectParentOrganizations) {
            LoginOrganizationInfo loginOrganizationInfo=new LoginOrganizationInfo();
            loginOrganizationInfo.setOrgGuid(objectParentOrganizations1.getOrg_guid());
            loginOrganizationInfo.setDisplayName(objectParentOrganizations1.getDisplay_name());
            loginOrganizationInfo.setAllPathName(objectParentOrganizations1.getAll_path_name());
            loginOrganizationInfo.setOrgClass(objectParentOrganizations1.getOrg_class());
            loginOrganizationInfo.setOriginalSort(objectParentOrganizations1.getOriginal_sort());
            loginOrganizationInfo.setParentGuid(objectParentOrganizations1.getParent_guid());
            loginOrganizationInfo.setUserGuid(objectDeatil.getUser_guid());
            list.add(loginOrganizationInfo);
        }
        loginUserInfo.setLoginOrganizationInfoList(list);
        return  loginUserInfoService.save(loginUserInfo);

    }


    public long  addPageBrowsing(BasicPageBrowsing basicPageBrowsing, HttpServletRequest request) {
        basicPageBrowsing.setBrowsingDate(new Date());
        basicPageBrowsing.setIpAddress(IPUtils.findIp(request));
        long save = basicPageBrowsingService.save(basicPageBrowsing);
        System.out.println(save);
        return  save;


    }
}
